package com.lc.activiti.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.activiti.config.ActivitiKeys;
import com.lc.activiti.config.Result;
import com.lc.activiti.process.service.TaskActionService;
import com.lc.activiti.sys.dao.SysLeaveDao;
import com.lc.activiti.sys.entity.SysAudit;
import com.lc.activiti.sys.entity.SysLeave;
import com.lc.activiti.sys.service.SysAuditService;
import com.lc.activiti.sys.service.SysLeaveService;
import com.lc.activiti.utils.JwtUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 请假表(SysLeave)表服务实现类
 *
 * @author 骆超
 * @since 2021-06-21 17:40:53
 */
@Service("sysLeaveService")
public class SysLeaveServiceImpl extends ServiceImpl<SysLeaveDao, SysLeave> implements SysLeaveService {

    @Resource
    private TaskActionService taskActionService;
    @Resource
    private SysAuditService auditService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveLeaveInfo(SysLeave sysLeave) throws IOException {
        //首先保存请假信息
        sysLeave.insertOrUpdate();
        //如果是提交，则进入流程中
        if (sysLeave.getSaveStatus()==2){
            //如果是已有id的情况下，将上次正常使用的审核记录舍弃，不再记录状态
            if (ObjectUtil.isNotEmpty(sysLeave.getId())){
                SysAudit sysAudit = auditService.getOne(Wrappers.lambdaQuery(SysAudit.class).eq(SysAudit::getEnable,1).eq(SysAudit::getBusinessId, sysLeave.getId()));
                if (ObjectUtil.isNotEmpty(sysAudit)){
                    sysAudit.setEnable(0).updateById();
                }
            }
            taskActionService.startTaskByBusiness(sysLeave.getId(), ActivitiKeys.LEAVE.getKey(),ActivitiKeys.LEAVE.getProcessName(),JSONUtil.parseObj(sysLeave));
        }
        return Result.success(sysLeave);
    }

    @Override
    public Result getLeavePage(SysLeave sysLeave) {
        Page<SysLeave> page = page(sysLeave.getPage(), Wrappers.lambdaQuery(SysLeave.class)
                .like(ObjectUtil.isNotEmpty(sysLeave.getReason()),SysLeave::getReason,sysLeave.getReason())
                .eq(SysLeave::getDelFlag, 1).eq(SysLeave::getCreateUser, JwtUtil.getUser().getId()).orderByDesc(SysLeave::getCreateDate));
        List<SysLeave> records = page.getRecords();
        if (!records.isEmpty()){
            //获取审核信息
            List<SysAudit> list = auditService.list(Wrappers.lambdaQuery(SysAudit.class).eq(SysAudit::getDelFlag, 1).eq(SysAudit::getEnable,1)
                    .in(SysAudit::getBusinessId, records.stream().map(SysLeave::getId).collect(Collectors.toList())));
            records.forEach(leave->{
                list.stream().filter(f->f.getBusinessId().equals(leave.getId())).findFirst().ifPresent(i->{
                    leave.setAuditStatus(i.getStatus());
                    leave.setAuditor(i.getAuditor());
                    leave.setProcessInstanceId(i.getProcessInstanceId());
                    leave.setProcessDefinitionId(i.getProcessDefinitionId());
                });
            });
        }
        return Result.success(page);
    }
}
